package com.isesol.ime.validator.message;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import com.isesol.ime.service.*;
import org.apache.commons.collections.*;
import org.apache.commons.lang3.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class MessageCopyValidator extends ValidatorHandler<MessageDTO> implements Validator<MessageDTO> {

    @Resource
    MessageMapper messageMapper;

    @Resource
    MessageService messageService;

    @Override
    public boolean validate(ValidatorContext context, MessageDTO messageDTO) {

        boolean result = true;


        if (null == messageDTO.getId()){

            context.addErrorMsg("params error,id is null");
            result =  false;
        }
        if (StringUtils.isEmpty(messageDTO.getMsgId())){

            context.addErrorMsg("params error,msgId is null");
            result =  false;
        }
        if (StringUtils.isEmpty(messageDTO.getMsgName())){

            context.addErrorMsg("params error,msgName is null");
            result =  false;
        }


        if (CollectionUtils.isNotEmpty(messageMapper.
                findByMsgIdAndStatus(messageDTO.getMsgId(), StatusEnum.ON.getStatus()))){

            context.addErrorMsg(ErrorCode.MESSAGEID_ALREADY_EXISTS.getMessage());
            result =  false;

        }

        if(StringUtils.isNotEmpty(String.valueOf(messageDTO.getId()))){

            MessageDO message = messageMapper.getById(messageDTO.getId());

            if (message == null){

                context.addErrorMsg(ErrorCode.MachineToolMessageCopyDTO_SOURCE_MSG_NOTFOUND.getMessage());
                result =  false;

            }
        }

        return result;
    }


}